import { Component, OnInit } from '@angular/core';
import {NzModalService} from 'ng-zorro-antd';

declare var form: any;
declare var security: any;

@Component({
  templateUrl: './security.person.component.html'
})
export class SecurityPersonComponent implements OnInit {

  ss: any;
  // 表单
  fs: any = null;
  ffs: any;
  // 字典
  positionDic: any = null;
  airportDic: any = null;
  workTypeDic: any = null;
  qualificationDic: any = null;
  reQualificationTime: any = null;
  constructor(private modal: NzModalService) {

    this.ss = security.SecurityService;
    this.ss.show = false;

    this.ffs = new form.FileService();
    // 注册表单
    this.fs = new form.FormSimpleService('securityperson', {
      orderObject: { id: 0 },
      keyField: 'id',
      randomFields: ['id'],
      // 加入导入配置中的配置编号
      inputid: 'abry',
      // Excel中导入数据的开始行(第一行为0)
      startrow: 1
    });
    this.fs.loadData(true);
    // 注册字典
    this.positionDic = new form.DictionaryService('p_position');
    this.airportDic = new form.DictionaryService('zd_airport');
    this.workTypeDic = new form.DictionaryService('p_work_type');
    this.workTypeDic.loadAll();
    this.qualificationDic = new form.DictionaryService('p_qualification');
    this.qualificationDic.loadAll();
  }

  editRow(item) {
    if (item) {
      window.open('#data_personinfo_mis?info=true&id=' + item.id);
    } else {
      window.open('#data_personinfo_mis?info=true');
    }
  }

  ngOnInit() {
  }

  loadData() {
    if (this.reQualificationTime) {
        this.fs.queryObject['reQualificationTime:>='] = this.dateFormat('YYYY-mm-dd', this.reQualificationTime);
        this.fs.queryObject['reQualificationTime:<='] = this.dateFormat('YYYY-mm-dd', this.reQualificationTime);
    }
    this.fs.loadData(true);
  }

  dateFormat(fmt, date) {
    let ret;
    const opt = {
      'Y+': date.getFullYear().toString(),        // 年
      'm+': (date.getMonth() + 1).toString(),     // 月
      'd+': date.getDate().toString(),            // 日
      'H+': date.getHours().toString(),           // 时
      'M+': date.getMinutes().toString(),         // 分
      'S+': date.getSeconds().toString()          // 秒
      // 有其他格式化字符需求可以继续添加，必须转化成字符串
    };
    // tslint:disable-next-line:forin
    for (const k in opt) {
      ret = new RegExp('(' + k + ')').exec(fmt);
      if (ret) {
        fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')));
      }
    }
    return fmt;
  }

  // 文件上传后处理
  excelHandleChange(obj) {
    this.fs.excelHandleChange(obj, () => {
      this.fs.loadData(true);
      this.modal.success({
        nzTitle: '系统提示',
        nzContent: '数据导入成功'
      });
    }, () => {
      this.modal.error({
        nzTitle: '系统提示',
        nzContent: '数据导入失败，详细请查看数据导入日志！'
      });
    });
  }
}
